﻿#include "CameraFactory.h"
#include "CameraPC_Qt6.h"
#include <QMediaDevices>
#include <QDebug>
namespace Qly{

QList<IndustryCameraInfo> CameraFactory::availableCameras()
{
    QList<Qly::IndustryCameraInfo> list;
    const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
    for (const QCameraDevice &cameraDevice : cameras)
    {
        Qly::IndustryCameraInfo info;
        info.type = Qly::CameraType::SYS_CAM;
        info.cameraID = cameraDevice.description();
        info.description = cameraDevice.description();
        list << info;
        qDebug() << info.cameraID;
    }
    return list;
}

IndustryCamera *CameraFactory::findCameraByName(QString name, QObject *parent)
{
    Qly::CameraPC_Qt6 * pCamera = new Qly::CameraPC_Qt6(parent);
    if( !pCamera->openCameraByName(name) )
    {
        pCamera = nullptr;
        qDebug() << "No System Camera Find";
    }

    return pCamera;
}

IndustryCamera *CameraFactory::defaultCamera(QObject *parent)
{
    Qly::CameraPC_Qt6 * pCamera = new Qly::CameraPC_Qt6(parent);
    if( !pCamera->openDefaultCamera() )
    {
        delete pCamera;
        pCamera = nullptr;
    }
    return pCamera;
}

QString CameraFactory::vendorName()
{
    return QString("System Camera");
}

bool CameraFactory::release(IndustryCamera *camera)
{
    Qly::CameraPC_Qt6 * pCamera = dynamic_cast<Qly::CameraPC_Qt6 *> (camera); // 能够转换成功说明是这个dll new出来的对象。可以删除。
    if(pCamera)
    {
        delete pCamera;
        return true;
    }
    return false; //说明不能删除这个对象
}

} //namespace Qly

Qly::CameraFactoryInterface *CreateInstance()
{
    Qly::CameraFactory * cameraFactory = new Qly::CameraFactory();
    return cameraFactory;
}

void Release(Qly::CameraFactoryInterface *p)
{
    delete p;
}

QList<Qly::IndustryCameraInfo> Qly::CameraFactory::availableCameras(CameraType type)
{
    if(type == CameraType::SYS_CAM)
    {
        return availableCameras();
    }
    return QList<Qly::IndustryCameraInfo>();
}

QList<Qly::IndustryCameraInfo> Qly::CameraFactory::availableCameras(QSet<CameraType> types)
{
    if( types.contains(CameraType::SYS_CAM) )
    {
        return availableCameras();
    }
    return QList<Qly::IndustryCameraInfo>();
}

